package com.lyh.mall.product.app;

import java.util.Arrays;
import java.util.List;

import com.lyh.common.utils.R;
import com.lyh.mall.product.entity.CategoryEntity;
import com.lyh.mall.product.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 商品三级分类
 *
 * @author lyh
 * @email 1964059406@qq.com
 * @date 2021-09-25 21:18:00
 */
@Api("商品三级分类")
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查出所有分类以及子分类，以树形结构组装起来
     */
    @ApiOperation(value = "查出所有分类以及子分类，以树形结构组装起来",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = R.class,
            notes = "查出所有分类以及子分类，以树形结构组装起来")
    @GetMapping("/list/tree")
    public R list(){
        List<CategoryEntity> entities = categoryService.listWithTree();
        return R.ok().put("data", entities);
    }


    /**
     * 信息
     */
    @ApiOperation(value = "根据catId查询信息")
    @GetMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存")
    @PostMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 批量修改
     */
    @ApiOperation(value = "批量修改")
    @PostMapping("/update/sort")
    public R updateSort(@RequestBody CategoryEntity[] category){
        categoryService.updateBatchById(Arrays.asList(category));
        return R.ok();
    }

    /**
     * 级联修改更新所有关联的数据
     */
    @ApiOperation(value = "级联修改更新所有关联的数据")
    @PostMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateCascade(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "检查当前删除的菜单，是否备别的地方引用")
    @PostMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
		//categoryService.removeByIds(Arrays.asList(catIds));
        //检查当前删除的菜单，是否备别的地方引用
        categoryService.removeMenuByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
